package com.letao.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.letao.server.dto.CartOrderDTO;
import com.letao.server.dto.CartOrderListDTO;
import com.letao.server.dto.Result;
import com.letao.server.entity.SkuInfo;
import com.letao.server.entity.SpuInfo;
import com.letao.server.entity.UserCartInfo;
import com.letao.server.mapper.UserCartInfoMapper;
import com.letao.server.service.ISkuService;
import com.letao.server.service.ISpuService;
import com.letao.server.service.IUserCartService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserCartServiceImpl extends ServiceImpl<UserCartInfoMapper, UserCartInfo> implements IUserCartService {
    @Resource
    ISkuService skuService;
    @Resource
    ISpuService spuService;

    @Override
    public Result cartOrderInfo(List<UserCartInfo> userInfoList) {
        Double totalMoney = .0;
        List<CartOrderListDTO> cartOrderListDTOS = new ArrayList<>();
        for (UserCartInfo info : userInfoList) {
            CartOrderListDTO cartOrderListDTO = new CartOrderListDTO();
            cartOrderListDTO.setNum(info.getNumber());
            cartOrderListDTO.setPrice(info.getCartPrice());
            totalMoney += cartOrderListDTO.getPrice() * cartOrderListDTO.getNum();
            cartOrderListDTO.setSkuName(info.getSkuName());
            SkuInfo sku = skuService.getById(info.getSkuId());
            cartOrderListDTO.setSkuImage(sku.getHasImage());
            SpuInfo spu = spuService.getById(info.getSpuId());
            cartOrderListDTO.setSpuName(spu.getSpuName());
            cartOrderListDTO.setSpuImage(spu.getSpuImage());
            cartOrderListDTO.setSpuId(info.getSpuId());
            cartOrderListDTO.setSkuId(info.getSkuId());
            cartOrderListDTOS.add(cartOrderListDTO);

        }

        CartOrderDTO cartOrderDTO = new CartOrderDTO();
        cartOrderDTO.setCartOrderList(cartOrderListDTOS);
        cartOrderDTO.setTotalPrice(totalMoney);
        return Result.success(cartOrderDTO);
    }
}
